﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace YXShop.Web.admin.stocks
{
    public partial class stocks_list : System.Web.UI.Page
    {
        BLL.Stocks.OutProduct bllOutProduct = new BLL.Stocks.OutProduct();
        BLL.Product.ProductInfo bllProductInfo = new BLL.Product.ProductInfo();
        BLL.Stocks.BarCode bllBarCode = new BLL.Stocks.BarCode();
        BLL.Store.Store bllStore = new BLL.Store.Store();
        BLL.Store.StoreCenter bllStoreCenter = new BLL.Store.StoreCenter();
        BLL.Admin.Administrators bllAdministrator = new BLL.Admin.Administrators();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtAreaStore.Text = "";
            this.txtStore.Text = "";
            if (this.hfStoreCenterid.Value.ToString() != "")
            {
                string[] arr = this.hfStoreCenterid.Value.ToString().Split(',');
                foreach (string str in arr)
                {
                    Model.Store.StoreCenter storecenterModel = bllStoreCenter.GetModelById(int.Parse(str));
                    if (storecenterModel != null)
                    {
                        if (this.txtAreaStore.Text != "")
                        {
                            this.txtAreaStore.Text += "," + storecenterModel.StoreCentername;
                        }
                        else
                        {
                            this.txtAreaStore.Text = storecenterModel.StoreCentername;
                        }
                    }
                }
            }
            if (this.hfStoreid.Value.ToString() != "")
            {
                string[] arr = this.hfStoreid.Value.ToString().Split(',');
                foreach (string str in arr)
                {
                    Model.Store.Store storeModel = bllStore.GetModelById(int.Parse(str));

                    if (storeModel != null)
                    {
                        if (this.txtStore.Text != "")
                        {
                            this.txtStore.Text += "," + storeModel.Storename;
                        }
                        else
                        {
                            this.txtStore.Text = storeModel.Storename;
                        }

                    }
                }
            }
            if (!IsPostBack)
            {
                
                string where = "";
                //YXShop.Common.PromptInfo.Popedom("014001001");
                YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
                if (admin == null)
                {
                    this.ltlMsg.Text = "用户操作失败，请重新登录";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                else
                {
                     where =getDataByAdmin(admin);
                     //string where2 = GetWhereByAdminid(admin);
                     //where = where + where2;
                }
                this.lblList.Text = GetList(where);
                InitWebControl();
                string s = GetProducts();
            }
        }
        #region 根据用户 获取库存where
        public string GetWhereByAdminid(YXShop.Model.Admin.AdminInfo admin)
        {
            string where = "";
            if (admin.AdminType == 1)//总部管理员
            { 
                
            }
            if (admin.AdminType == 2)//区域中心管理员
            {
               List<Model.Store.StoreCenter> listStoreCenter= bllAdministrator.GetStoreCenterAdmin_StoreCenter(admin.AdminId);
               if (listStoreCenter != null)
               {
                   if (listStoreCenter.Count > 0)
                   {
                       string ids = "";
                       foreach (Model.Store.StoreCenter sc in listStoreCenter)
                       {
                           if (ids == "")
                           {
                               ids = sc.Sid.ToString();
                           }
                           else
                           {
                               ids += "," + sc.Sid.ToString();
                           }
                       }
                       where = " and (stocktypeid = 1 and stockid in (" + ids + "))";
                   }
               }
              List<Model.Store.Store> listStore= bllAdministrator.GetStoreCenterAdmin_Store(admin.AdminId);
              if (listStore != null)
              {
                  if (listStore.Count > 0)
                  {
                      string ids = "";
                      foreach (Model.Store.Store s in listStore)
                      {
                          if (ids == "")
                          {
                              ids = s.Sid.ToString();
                          }
                          else
                          {
                              ids += "," + s.Sid.ToString();
                          }
                          where += " and (stocktypeid = 2 and stockid in (" + ids + "))";
                      }
                  }
              }
            }
            if (admin.AdminType == 3)//门店管理员
            {
                List<Model.Store.Store> listStore = bllAdministrator.GetStoreAdmin_Store(admin.AdminId);
                if (listStore != null)
                {
                    if (listStore.Count > 0)
                    {
                        string ids = "";
                        foreach (Model.Store.Store s in listStore)
                        {
                            if (ids == "")
                            {
                                ids = s.Sid.ToString();
                            }
                            else
                            {
                                ids += "," + s.Sid.ToString();
                            }
                            where = " and (stocktypeid = 2 and stockid in (" + ids + "))";
                        }
                    }
                }
            }
            return where;
        }
        #endregion
        protected void linkQuery_Click(object sender, EventArgs e)
        {
            string where = "";
            //string ids = GetProducts();
            //if (ids != "")
            //{
            //    where = "and productid in(" + ids + ") ";
            //}
            string where2 = "";
            if (this.DropDownList1.SelectedItem.Value != "4")//所有仓库
            {
                if (this.DropDownList1.SelectedItem.Value == "0")//中央仓库
                {
                    where2 = "and stocktypeid = 0";
                }
                else
                {
                    where2 = "and stocktypeid = " + this.DropDownList1.SelectedItem.Value;
                    if (this.DropDownList1.SelectedItem.Value == "1")
                    {
                        if (this.hfStoreCenterid.Value != "")
                        {
                            where2 += " and stockid in(" + this.hfStoreCenterid.Value + ")";
                        }
                    }
                    if (this.DropDownList1.SelectedItem.Value == "2")
                    {
                        if (this.hfStoreid.Value != "")
                        {
                            where2 += " and stockid in(" + this.hfStoreid.Value + ")";
                        }
                    }
                }
            }
            if (where2 != "")
            {
                where += where2;
            }
            if (this.DropDownListState.SelectedItem.Value != "-1")
            {
                where += " and stateid=" + this.DropDownListState.SelectedItem.Value;
            }
            this.lblList.Text = GetList(where);
        }
        #region 绑定
        private string GetList(string where)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            ChangeHope.DataBase.DataByPage dataPage = bllBarCode.GetList("and (stateid=1 or stateid=4) " + where);
            table.AddHeadCol("", "<input id=\"chall\" type=\"checkbox\" onclick=\"SelectAll(chall)\" />全选");
            table.AddHeadCol("", "商品名称");
            table.AddHeadCol("", "条码编号");
            table.AddHeadCol("", "仓库类型");
            table.AddHeadCol("", "仓库名称");
            table.AddHeadCol("", "入库时间");
            table.AddHeadCol("", "出库时间");
            table.AddHeadCol("", "状态");
            table.AddHeadCol("", "备注");
            table.AddHeadCol("", "操作");
            table.AddRow();
            if (dataPage.DataReader != null)
            {
                int curpage = ChangeHope.WebPage.PageRequest.GetInt("pageindex");
                if (curpage < 0)
                {
                    curpage = 1;
                }
                int count = 0;
                //YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();

                while (dataPage.DataReader.Read())
                {
                    int id=Convert.ToInt32(dataPage.DataReader["id"]);
                    count++;
                    string No = (15 * (curpage - 1) + count).ToString();
                    table.AddCol("<input id=\"cbTm\"  type=\"checkbox\" value=\"" + dataPage.DataReader["id"].ToString() + "\" />");
                    Model.Stocks.BarCode barCode = bllBarCode.GetModelById(id);
                    int productid = Convert.ToInt32(dataPage.DataReader["productid"]);
                    table.AddCol(bllProductInfo.GetModel(productid).pro_Name);
                    table.AddCol(dataPage.DataReader["barcode"].ToString());
                    string stocktype = "";
                    if (barCode.StockTypeID == Model.Stocks.StockTypeEnum.CenterStock)
                    {
                        stocktype = "中央仓库";
                    }
                    if (barCode.StockTypeID == Model.Stocks.StockTypeEnum.AreaStock)
                    {
                        stocktype = "区域中心";
                    }
                    if (barCode.StockTypeID == Model.Stocks.StockTypeEnum.ShopStock)
                    {
                        stocktype = "门店仓库";
                    }
                    table.AddCol(stocktype);
                    string stockName = "";
                    if ((int)barCode.StockTypeID == 0)//中央仓库
                    {
                        stockName = "中央仓库";
                    }
                    if((int)barCode.StockTypeID == 1)//区域中心
                    {

                        stockName=bllStoreCenter.GetModelById(barCode.StockID).StoreCentername;
                    }
                       if((int)barCode.StockTypeID == 2)//门店
                    {
                        stockName = bllStore.GetModelById(barCode.StockID).Storename;
                    }
                    table.AddCol(stockName);
                    table.AddCol(barCode.InTime.ToString());
                    table.AddCol(barCode.OutTime.ToString());
                    string statestr = "";
                    if (barCode.StateID == Model.Stocks.ProductStateEnum.Order)
                    {
                        statestr = "订购";
                    }
                    if (barCode.StateID == Model.Stocks.ProductStateEnum.Deleted)
                    {
                        statestr = "删除";
                    }
                    if (barCode.StateID == Model.Stocks.ProductStateEnum.Putin)
                    {
                        statestr = "入库";
                    }
                    if (barCode.StateID == Model.Stocks.ProductStateEnum.Putout)
                    {
                        statestr = "出库";
                    }
                    if (barCode.StateID == Model.Stocks.ProductStateEnum.Returned)
                    {
                        statestr = "退还";
                    }
                    if (barCode.StateID == Model.Stocks.ProductStateEnum.Selled)
                    {
                        statestr = "出售";
                    }
                    table.AddCol(statestr);
                    table.AddCol(barCode.Notes);
                    string strUrl = YXShop.TemplateAction.Common.UrlStr("Out_Stocks_Edit", "id=" + dataPage.DataReader["id"].ToString(), "admin/stocks/");
                    table.AddCol(string.Format("<a href=\"" + strUrl + "\">出库/退货</a> <a href='../../product/productcontent.aspx?q_productid={0}' target='_blank'>浏览</a>", productid));
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
        #endregion

        #region  根据用户获取数据 
        public string getDataByAdmin(Model.Admin.AdminInfo admin)
        {
            string result = "";
            if (admin.AdminType == 1)//中央仓库管理员
            {
                result = "";
            }
            if (admin.AdminType == 2)//区域仓库管理员
            {
                this.DropDownList1.Items.RemoveAt(0);
                List<Model.Store.StoreCenter> list=bllAdministrator.GetStoreCenterAdmin_StoreCenter(admin.AdminId);
                List<Model.Store.Store> listStore=bllAdministrator.GetStoreCenterAdmin_Store(admin.AdminId);
                string ids = "";//所管理的区域中心
                string ids2="";//所管理的门店
                if (list != null)
                {
                    if (list.Count > 0)
                    {
                        foreach (Model.Store.StoreCenter center in list)
                        {
                            if (ids == "")
                            {
                                ids = center.Sid.ToString();
                            }
                            else
                            {
                                ids += "," + center.Sid.ToString();
                            }
                        }
                    }
                }
                if(listStore!=null)
                {
                    if(listStore.Count>0)
                    {
                        foreach (Model.Store.Store store in listStore)
                        {
                            if (ids2 == "")
                            {
                                ids2 = store.Sid.ToString();
                            }
                            else
                            {
                                ids2 += "," + store.Sid.ToString();
                            }
                        }
                    }
                }
                if (ids != "")
                {
                    result = "and (stocktypeid=1 and stockid in(" + ids + ")) ";
                }
                if (ids2 != "")
                {
                    result += "and (stocktypeid=2 and stockid in(" + ids2 + ")) ";
                }
              
            }
            if (admin.AdminType == 3)//名店管理员
            {
                this.DropDownList1.Items.RemoveAt(0);
                this.DropDownList1.Items.RemoveAt(0);
                List<Model.Store.Store> listStore = bllAdministrator.GetStoreCenterAdmin_Store(admin.AdminId);
                string ids2 = "";//所管理的门店
                if (listStore != null)
                {
                    if (listStore.Count > 0)
                    {
                        foreach (Model.Store.Store store in listStore)
                        {
                            if (ids2 == "")
                            {
                                ids2 = store.Sid.ToString();
                            }
                            else
                            {
                                ids2 += "," + store.Sid.ToString();
                            }
                        }
                    }
                }
                if (ids2 != "")
                {
                    result = "and (stocktypeid=2 and stockid in()" + ids2 + ")) ";
                }
            }
            return result;
        }
        #endregion
        #region 搜索设置
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            this.txtAreaStore.Attributes.Add("readonly", "readonly");
            this.txtAreaStore.Attributes.Add("onclick", "selectFile('Storecenterlist',new Array(" + this.hfStoreCenterid.ClientID + "," + this.txtAreaStore.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            this.txtStore.Attributes.Add("readonly", "readonly");
            this.txtStore.Attributes.Add("onclick", "selectFile('Storelist',new Array(" + this.hfStoreid.ClientID + "," + this.txtStore.ClientID + "),310,450,'" + sp.DummyPaht + "');");
        }
        #endregion
        #region
        public string GetProducts()
        {
            string ids = "";
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Product.ProductInfo data = new YXShop.BLL.Product.ProductInfo();
            ChangeHope.DataBase.DataByPage dataPage = data.GetList("[order by] pro_ID desc", 20, " and pro_PutoutType=0");
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    if (ids == "")
                    {
                        ids = dataPage.DataReader["pro_ID"].ToString();
                    }
                    else
                    {
                        ids =ids+ "," + dataPage.DataReader["pro_ID"].ToString();
                    }
                }
            }
            dataPage.Dispose();
            return ids;
        }
        
        #endregion

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfStoreCenterid.Value = "";
            this.hfStoreid.Value = "";
            if (DropDownList1.SelectedItem.Value == "4")
            {
                
                // 所有库存
                this.txtCenter.Visible = true;
                this.txtCenter.Text = "所有仓库";
                this.txtAreaStore.Visible = false;
                this.txtStore.Visible = false;
            }
            if (DropDownList1.SelectedItem.Value == "0")
            {
                //中央仓库库存
              
                this.txtCenter.Visible = true;
                this.txtCenter.Text = "中央仓库";
                this.txtAreaStore.Visible = false;
                this.txtStore.Visible = false;
            }
            if (DropDownList1.SelectedItem.Value == "1")
            {
                //区域中心库存
            
                this.txtAreaStore.Visible = true;
                this.txtStore.Visible = false;
                this.txtCenter.Visible = false;
                
            }
            if (DropDownList1.SelectedItem.Value == "2")
            {
                //门店库存
               
                this.txtAreaStore.Visible = false;
                this.txtStore.Visible = true;
                this.txtCenter.Visible = false;
            }
            //this.hf_where.Value = where;
            linkQuery_Click(sender, e);
        }
    }
}